﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ATM_Software
{
    class TransactionDAL
    {
        StreamReader reader;
        StreamWriter writer;
        private const string TRANSACTION_FILE = "..\\..\\transactions.txt";
        public bool Transaction( TransactionData transaction)
        {            
            using (writer =  new StreamWriter(TRANSACTION_FILE,true))
            {                    
                writer.WriteLine(transaction.ToTransactionString());                
            }
            return true;
        }

        public  List<TransactionData> GetAllTransactions()
        {
            List<TransactionData> allTransactions = new List<TransactionData>();
            TransactionData tempTransaction;
            string[] tokens = null;
            if (File.Exists(TRANSACTION_FILE))
            {
                string[] allRecords = File.ReadAllLines(TRANSACTION_FILE);
                foreach (string record in allRecords)
                {
                    tokens = record.Split(';');
                    tempTransaction = new TransactionData();
                    tempTransaction.AccountId = int.Parse(tokens[0]);
                    tempTransaction.TransactionType = tokens[1];
                    tempTransaction.Ammount = int.Parse(tokens[2]);
                    tempTransaction.Date = tokens[3];
                    allTransactions.Add(tempTransaction);
                }
                return allTransactions;
            }
            return null;
        }
    }
}
